#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/12/21 10:05
# @Author  : Aries
# @Site    : 
# @File    : dem_tools.py.py
# @Software: PyCharm Community Edition

import config
from shape_tools import *

def _Maxs2Cutline(Points):
    """
    将极大值点连成一条线
    :param Points:
    :return:
    """
    pass

def _get_local_maxima(line):
    """
    提取一条断面线上的极大值点
    :param line:
    :return: 返回极大值点的集合
    """
    from scipy.signal import argrelextrema

    line = np.asarray(line)
    maxID =  argrelextrema(line, np.greater)
    return maxID


def LCR_detection():
    """
    对三条输电线L C R的地面安全性做评估
    :return:
    """
    # Driver: GTiff / GeoTIFF
    # Files: P:\DEM_Tools\data\test\test.tif
    # Size is 1939, 233
    # Coordinate
    # System is:
    # PROJCS["WGS 84 / UTM zone 49N"

    pass

def L_detection():
    """
    对输电线L地面安全性做评估
    :return:
    """
    # Driver: GTiff / GeoTIFF
    # Files: P:\DEM_Tools\data\test\test.tif
    # Size is 1939, 233
    # Coordinate
    # System is:
    # PROJCS["WGS 84 / UTM zone 49N"

    dem = "{}\\test.tif".format(config.dataDir)
    #print dem
    points = {'p1': (538822, 3651886), 'p2': (539596, 3651912), 'p3':(540605, 3651924)}
    shp = "{}\\test2.shp".format(config.shpDir)
    #print shp
    z = shp2points(shp, dem)
    #print len(z)
    z = np.asarray(z)
    np.savetxt("{}\\line.csv".format(config.outDir), z)
    maxID = _get_local_maxima(z)
    print maxID[0][9]
    r = z[maxID]
    _r = np.zeros_like(z)
    print len(_r)
    for i in range(len(_r)):
        _r[i] = -99
        j = 0
        print i
        for j in range(len(maxID[0])):
            if i == maxID[0][j]:
                _r[i] = z[i]


    np.savetxt("{}\\maxs.csv".format(config.outDir), np.asarray(r))
    import matplotlib.pyplot as plt
    plt.plot(_r)
    plt.plot(z)
    plt.show()

if __name__ == '__main__':
    L_detection()
